package com.ruoyi.alarm.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.alarm.mapper.AlarmTypeMapper;
import com.ruoyi.alarm.domain.AlarmType;
import com.ruoyi.alarm.service.IAlarmTypeService;

import javax.annotation.PostConstruct;

/**
 * 告警类型Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-03-10
 */
@Service
public class AlarmTypeServiceImpl implements IAlarmTypeService 
{
    public static final String CACHE_MAP = "alarm_type";
    private static final Logger log = LoggerFactory.getLogger(AlarmTypeServiceImpl.class);

    @Autowired
    private AlarmTypeMapper alarmTypeMapper;
    @Autowired
    private RedisCache redisCache;

    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }

    /**
     * 查询告警类型
     *
     * @param alarmTypeCode 告警类型代码
     * @return 告警类型
     */
    @Override
    public AlarmType selectAlarmType(String alarmTypeCode){
        AlarmType alarmType = redisCache.getCacheMapValue(CACHE_MAP, alarmTypeCode);
        if(alarmType != null){
            return alarmType;
        }

        alarmType = alarmTypeMapper.selectAlarmTypeByCode(alarmTypeCode);
        if(alarmType != null){
            redisCache.setCacheMapValue(CACHE_MAP, alarmTypeCode, alarmType);
        }
        return alarmType;
    }

    /**
     * 查询告警类型
     * 
     * @param id 告警类型主键
     * @return 告警类型
     */
    @Override
    public AlarmType selectAlarmTypeById(Long id)
    {
        return alarmTypeMapper.selectAlarmTypeById(id);
    }

    /**
     * 查询告警类型列表
     * 
     * @param alarmType 告警类型
     * @return 告警类型
     */
    @Override
    public List<AlarmType> selectAlarmTypeList(AlarmType alarmType)
    {
        return alarmTypeMapper.selectAlarmTypeList(alarmType);
    }

    /**
     * 新增告警类型
     * 
     * @param alarmType 告警类型
     * @return 结果
     */
    @Override
    public int insertAlarmType(AlarmType alarmType)
    {
        AlarmType old = selectAlarmType(alarmType.getAlarmCode());
        if(old != null){
            throw new ServiceException("告警类型已存在:"+alarmType.getAlarmCode());
        }
        alarmType.setCreateTime(DateUtils.getNowDate());
        return alarmTypeMapper.insertAlarmType(alarmType);
    }

    /**
     * 修改告警类型
     * 
     * @param alarmType 告警类型
     * @return 结果
     */
    @Override
    public int updateAlarmType(AlarmType alarmType)
    {
        AlarmType old = selectAlarmType(alarmType.getAlarmCode());
        if(old != null && !old.getId().equals(alarmType.getId())){
            throw new ServiceException("告警代码不能重复:"+alarmType.getAlarmCode());
        }
        alarmType.setUpdateTime(DateUtils.getNowDate());
        return alarmTypeMapper.updateAlarmType(alarmType) + reCache();
    }

    /**
     * 批量删除告警类型
     * 
     * @param ids 需要删除的告警类型主键
     * @return 结果
     */
    @Override
    public int deleteAlarmTypeByIds(Long[] ids)
    {
        return alarmTypeMapper.deleteAlarmTypeByIds(ids) + reCache();
    }

    /**
     * 删除告警类型信息
     * 
     * @param id 告警类型主键
     * @return 结果
     */
    @Override
    public int deleteAlarmTypeById(Long id)
    {
        return alarmTypeMapper.deleteAlarmTypeById(id) + reCache();
    }


}
